import { defineStore } from 'pinia';
import { getMenuItemsByIds } from '@/router/menu';
export const userData = defineStore('user', {
    state: () => ({
      imgHead: sessionStorage.getItem('imgHead') || '../assets/img/img.jpg', //头像
      imgDecoration: sessionStorage.getItem('imgDecoration') || '../assets/img/ucenter-bg.jpg', //头像
      sex: "", //头像
      role: sessionStorage.getItem('role') || '',
      name: sessionStorage.getItem('name') || '', // 用户名
      permiss: sessionStorage.getItem('permiss') || '', // 用户菜单权限
      permissArray: getMenuItemsByIds( JSON.parse(sessionStorage.getItem('permiss')) || [] ) ||[], // 用户菜单权限
      unReadMessageNum:  0, //消息通知的数量
      unReadMessage: [], //未读消息通知数据
    }),
    actions: {
      updateSex(sex) {
        this.sex = sex;
      },
      updateUnReadMessageNum(newMessageNum) {
        this.unReadMessageNum = newMessageNum;
      },
      updateUnReadMessage(newMessage) {
        this.unReadMessage = newMessage;
      },
      updateImgHead(newUrl) {
        this.imgHead = newUrl;
        sessionStorage.setItem('imgHead', newUrl);
      },
      updateImgDecoration(newImgDecoration) {
        this.imgDecoration = newImgDecoration;
        sessionStorage.setItem('imgDecoration', newImgDecoration);
      },
      updateName(newName) {
        this.name = newName;
        sessionStorage.setItem('name', newName); 
      },
      updatePermiss(newPermiss) {
        this.permiss = newPermiss;
        let newPermissArray = getMenuItemsByIds( JSON.parse(newPermiss) || [] );
        this.permissArray = newPermissArray;
        sessionStorage.setItem('permiss', newPermiss);
      },
      updateAll(newName, newUrl, newPermiss, newImgDecoration,role) { 
        this.permiss = newPermiss;
        this.name = newName;
        this.imgHead = newUrl;
        let newPermissArray = getMenuItemsByIds( JSON.parse(newPermiss) || [] );
        this.permissArray = newPermissArray;
        this.imgDecoration = newImgDecoration;
        this.role = role;
        sessionStorage.setItem('imgDecoration', newImgDecoration);
        sessionStorage.setItem('permiss', newPermiss); 
        sessionStorage.setItem('name', newName); 
        sessionStorage.setItem('imgHead', newUrl);
        sessionStorage.setItem('role', role);
      },
      removeImgHead() {
        this.imgHead = '../assets/img/img.jpg'; // 设置默认值
        sessionStorage.removeItem('imgHead');
      },
      removeName() {
          this.name = '';
          sessionStorage.removeItem('name');
      },
      removePermiss() {
          this.permiss = '';
          sessionStorage.removeItem('permiss');
      },
      removeAll() {
          this.imgHead = '../assets/img/img.jpg'; // 设置默认值
          this.name = '';
          this.permiss = '';
          sessionStorage.removeItem('imgHead');
          sessionStorage.removeItem('name');
          sessionStorage.removeItem('permiss');
      },
    },
  })